//
//  TemperatureTableViewCell.m
//  WWTQ
//
//  Created by JP on 2018/10/30.
//  Copyright © 2018年 Jqert. All rights reserved.
//

#import "TemperatureTableViewCell.h"

#import "ForecastModel.h"

@interface TemperatureTableViewCell ()

@property (nonatomic, strong) UILabel *temperature; //温度
@property (nonatomic, strong) UIImageView *tqImageView; //天气图片
@property (nonatomic, strong) UILabel *tqLabel; //天气
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UILabel *notice;  //通知

@end

@implementation TemperatureTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI {
    [self.contentView addSubview:self.temperature];
    [self.contentView addSubview:self.notice];
    [self.contentView addSubview:self.tqImageView];
    [self.contentView addSubview:self.tqLabel];
    [self.contentView addSubview:self.dateLabel];
    
    self.dateLabel.text = [self getCurrentWeekDay];
    
    [self setUpLayoutWithMasonry];
}

- (void)setDataModel:(DataObjectModel *)dataModel {
    _dataModel = dataModel;
    
    self.temperature.text = [NSString stringWithFormat:@"%@°", dataModel.wendu];
    ForecastModel *model = dataModel.forecast[0];
    self.notice.text = model.notice;
    self.tqLabel.text = model.type;
//    self.dateLabel.text = model.date;
    NSString *imageName = [UIImageView addImageName:model.type];
    self.tqImageView.image = [UIImage imageNamed:imageName];
}

- (void)setUpLayoutWithMasonry {
    [self.notice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(20);
        make.right.equalTo(self.contentView).offset(-20);
        make.height.mas_equalTo(20);
        make.bottom.equalTo(self.contentView);
    }];
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(20);
        make.right.equalTo(self.contentView).offset(-20);
        make.height.mas_equalTo(20);
        make.bottom.equalTo(self.notice.mas_top).offset(-5);
    }];
    [self.tqImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(20);
        make.bottom.equalTo(self.dateLabel.mas_top).offset(-5);
        make.size.mas_equalTo(CGSizeMake(50, 20));
    }];
    [self.tqLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tqImageView.mas_right).offset(10);
        make.right.equalTo(self.contentView).offset(-20);
        make.centerY.mas_equalTo(self.tqImageView);
        make.height.mas_equalTo(20);
    }];
    [self.temperature mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(20);
        make.right.equalTo(self.contentView).offset(-20);
        make.bottom.equalTo(self.tqImageView.mas_top).offset(-10);
        make.height.mas_equalTo(65);
    }];
   
}

- (UILabel *)temperature {
    if (!_temperature) {
        _temperature = [[UILabel alloc] init];
        _temperature.textColor = [UIColor whiteColor];
        _temperature.font = [UIFont fontWithName:@"AppleGothic" size:65.0];
        _temperature.text = @"暂无";
    }
    return _temperature;
}

- (UIImageView *)tqImageView {
    if (!_tqImageView) {
        _tqImageView = [[UIImageView alloc] init];
        _tqImageView.clipsToBounds = YES;
        _tqImageView.contentMode = UIViewContentModeScaleAspectFit;
        _tqImageView.image = [UIImage imageNamed:@"wushuju"];
    }
    return _tqImageView;
}

- (UILabel *)notice {
    if (!_notice) {
        _notice = [[UILabel alloc] init];
        _notice.textColor = [UIColor whiteColor];
        _notice.font = ZYT14Font;
        _notice.text = @"愿你拥有比阳光明媚的心情";
    }
    return _notice;
}

- (UILabel *)tqLabel {
    if (!_tqLabel) {
        _tqLabel = [[UILabel alloc] init];
        _tqLabel.textColor = [UIColor whiteColor];
        _tqLabel.font = ZYT14Font;
        _tqLabel.text = @"暂无";
    }
    return _tqLabel;
}

- (UILabel *)dateLabel {
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc] init];
        _dateLabel.textColor = [UIColor whiteColor];
        _dateLabel.font = ZYT14Font;
        _dateLabel.text = @"暂无";
    }
    return _dateLabel;
}

//获取当前的时间
- (NSString*)getCurrentTimes{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    [formatter setDateFormat:@"YYYY-MM-dd"];
    //现在时间,你可以输出来看下是什么格式
    NSDate *datenow = [NSDate date];
    //----------将nsdate按formatter格式转成nsstring
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    return currentTimeString;
}

//获取星期几
- (NSString *)getWeekDayFordate:(NSTimeInterval)data {
    NSArray *weekday = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
    
    NSDate *newDate = [NSDate dateWithTimeIntervalSince1970:data];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:newDate];
    
    NSString *weekStr = [weekday objectAtIndex:components.weekday];
    NSString *currentDate = [self getCurrentTimes];
    
    return [NSString stringWithFormat:@"%@ %@", currentDate, weekStr];
}

- (NSString*)getCurrentWeekDay{
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
    return [self getWeekDayFordate:interval];
}



@end
